package com.future.baselib.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.R;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.baselib.utils.FLog;
import com.future.baselib.utils.ToastUtils;
import com.future.baselib.view.LoadingDialog;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 跳转方法
 * 接收传值方法
 * Android 4.4版本以上沉浸式的封装
 * Toolbar的封装
 * 界面之间消息传递的封装（EventBus）
 * 加载中动画的封装
 * Toast
 * Created by jniu on 2017/6/3.
 */

public abstract class BaseActivity extends AppCompatActivity {


    protected FrameLayout titleBar;
    protected TextView title;
    protected TextView tvTitleLeft;
    protected ImageView ivTitleLeft;
    protected TextView tvTitleRight;
    protected ImageView ivTitleRight;
    protected FrameLayout layoutContent;

    private LoadingDialog dialog;
    protected ToastUtils toast;

    protected Context mContext;
    protected View titleLine;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        StatusBarUtil.setColor(this, Color.WHITE);
        mContext = this;

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            getBundleExtras(extras);
        }

        toast = new ToastUtils(this);


    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base_with_title);
        StatusBarUtil.setTranslucent(this, 0x30);
        layoutContent = $(R.id.layout_content);
        View contentView = LayoutInflater.from(this).inflate(layoutResID, null);
        layoutContent.addView(contentView);

        titleBar = $(R.id.layout_title);
        if (titleBar != null) {
            initTitle();
        }
        StatusBarUtil.setTranslucent(this,0x30);
    }

    protected void isShowTitle(boolean showTitle) {
        titleBar.setVisibility(showTitle ? View.VISIBLE : View.GONE);
    }

    public <T extends View> T $(@IdRes int viewId) {
        return findViewById(viewId);
    }

    /**
     * 跳转
     *
     * @param activityClass
     */
    public void startActivity(Class activityClass) {
        startActivity(activityClass, null);
    }

    public void startActivity(Class activityClass, Bundle bundle) {
        Intent intent = new Intent(this, activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void startActivityForResult(Class activityClass, int requestCode) {
        startActivityForResult(activityClass, null, requestCode);
    }

    public void startActivityForResult(Class activityClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 加载对话框
     */
    public void showLoadingDialog() {
        if (dialog == null) {
            dialog = new LoadingDialog(this);
        }
        dialog.show();
    }

    public void dissmissLoadingDialog() {
        if (dialog != null) {
            dialog.close();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * Bundle  传递数据
     *
     * @param extras
     */
    protected void getBundleExtras(Bundle extras) {
    }

    protected void initTitle() {
        title = (TextView) findViewById(R.id.tv_title);
        ivTitleLeft = (ImageView) findViewById(R.id.iv_left);
        ivTitleRight = (ImageView) findViewById(R.id.iv_right);
        tvTitleRight = (TextView) findViewById(R.id.tv_right);
        tvTitleLeft = (TextView) findViewById(R.id.tv_left);
        titleLine = findViewById(R.id.view_line);
//        if (titleLine != null) {
//            titleLine.setVisibility(View.GONE);
//        }
        if (null != ivTitleLeft) {
            ivTitleLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    public void setTitleLeft(int drawable, View.OnClickListener listener) {
        ivTitleLeft.setImageResource(drawable);
        ivTitleLeft.setOnClickListener(listener);
        ivTitleLeft.setVisibility(View.VISIBLE);
        tvTitleLeft.setVisibility(View.GONE);
    }

    public void setTitleRight(int drawable, View.OnClickListener listener) {
        ivTitleRight.setImageResource(drawable);
        ivTitleRight.setOnClickListener(listener);
        ivTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setVisibility(View.GONE);
    }

    public void setTitleRight(String str, View.OnClickListener listener) {
        tvTitleRight.setText(str);
        tvTitleRight.setOnClickListener(listener);
        tvTitleRight.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
    }

    public void setTitleLeft(String str, View.OnClickListener listener) {
        tvTitleLeft.setText(str);
        tvTitleLeft.setOnClickListener(listener);
        tvTitleLeft.setVisibility(View.VISIBLE);
        ivTitleLeft.setVisibility(View.GONE);
    }

    public void setTitle(String title) {
        if (null != this.title)
            this.title.setText(title);
    }

    public void setTitle(int resId) {
        if (null != this.title) {
            this.title.setText(resId);
        }
    }

    public void toast(String msg) {
        toast.show(msg);
    }

    public void loge(String tag, String msg) {
        FLog.e(msg);
    }

    public void showKeyWord() {
        /** 弹出软键盘 **/
        try {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(MessageEvent event) {
    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(MessageEvent event) {
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }
}
